package org.frankkie.parcdroidprj;

/* Copyright (c) 2009 the authors listed at the following URL, and/or
the authors of referenced articles or incorporated external code:
http://en.literateprograms.org/Dijkstra's_algorithm_(Java)?action=history&offset=20081113161332

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Retrieved from: http://en.literateprograms.org/Dijkstra's_algorithm_(Java)?oldid=15444
 */
import java.util.PriorityQueue;

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

public class Dijkstra {

  public static void computePaths(OverlayPunt source) {

    ///RESETTEN
    for (int a=0; a<OverlayDinges.getGpsPlekken().size(); a++){
      OverlayDinges.getGpsPlekken().get(a).resetDijkstra();
    }
    for (int b=0; b<OverlayDinges.getDeLijnen().size(); b++){
      OverlayDinges.getDeLijnen().get(b).resetVoorDijkstra();
    }

    source.minDistance = 0.0;
    PriorityQueue<OverlayPunt> OverlayPuntQueue = new PriorityQueue<OverlayPunt>();
    OverlayPuntQueue.add(source);

    while (!OverlayPuntQueue.isEmpty()) {
      OverlayPunt u = OverlayPuntQueue.poll();
      System.out.println("Dijkstra.computePaths: while (!OverlayPuntQueue.isEmpty()) {");
      // Visit each OverlayLijn exiting u
      for (OverlayLijn e : u.adjacencies) {
        //OverlayPunt v = e.target;
        OverlayPunt v;
        if (e.punt1.equals(u)) {
          v = e.punt2;
        } else {
          v = e.punt1;
        }

        double weight = e.lengteLijn;
        double distanceThroughU = u.minDistance + weight;
        if (distanceThroughU < v.minDistance) {
          OverlayPuntQueue.remove(v);

          v.minDistance = distanceThroughU;
          v.previous = u;
          OverlayPuntQueue.add(v);

        }

      }
    }
  }

  public static List<OverlayPunt> getShortestPathTo(OverlayPunt target) {
    List<OverlayPunt> path = new ArrayList<OverlayPunt>();
    for (OverlayPunt OverlayPunt = target; OverlayPunt != null; OverlayPunt = OverlayPunt.previous) {
      path.add(OverlayPunt);
    }

    Collections.reverse(path);
    return path;
  }

  public static void startDijkstra() {
//    OverlayPunt v0 = new OverlayPunt("Harrisburg");
//    OverlayPunt v1 = new OverlayPunt("Baltimore");
//    OverlayPunt v2 = new OverlayPunt("Washington");
//    OverlayPunt v3 = new OverlayPunt("Philadelphia");
//    OverlayPunt v4 = new OverlayPunt("Binghamton");
//    OverlayPunt v5 = new OverlayPunt("Allentown");
//    OverlayPunt v6 = new OverlayPunt("New York");
//    v0.adjacencies = new OverlayLijn[]{new OverlayLijn(v1, 79.83),
//              new OverlayLijn(v5, 81.15)};
//    v1.adjacencies = new OverlayLijn[]{new OverlayLijn(v0, 79.75),
//              new OverlayLijn(v2, 39.42),
//              new OverlayLijn(v3, 103.00)};
//    v2.adjacencies = new OverlayLijn[]{new OverlayLijn(v1, 38.65)};
//    v3.adjacencies = new OverlayLijn[]{new OverlayLijn(v1, 102.53),
//              new OverlayLijn(v5, 61.44),
//              new OverlayLijn(v6, 96.79)};
//    v4.adjacencies = new OverlayLijn[]{new OverlayLijn(v5, 133.04)};
//    v5.adjacencies = new OverlayLijn[]{new OverlayLijn(v0, 81.77),
//              new OverlayLijn(v3, 62.05),
//              new OverlayLijn(v4, 134.47),
//              new OverlayLijn(v6, 91.63)};
//    v6.adjacencies = new OverlayLijn[]{new OverlayLijn(v3, 97.24),
//              new OverlayLijn(v5, 87.94)};
//    OverlayPunt[] vertices = {v0, v1, v2, v3, v4, v5, v6};

    System.out.println("Dijkstra.startDijkstra()");
    OverlayPunt src = OverlayDinges.getDeLijnen().get(OverlayDinges.lijnDieHetDichtsteBijIs).punt1;
    computePaths(src);
    //for (OverlayPunt v : vertices) {
    System.out.println("Aantal GPS-Plekken: " + OverlayDinges.getGpsPlekken().size());
    for (int a = 0; a < OverlayDinges.getGpsPlekken().size(); a++) {
      OverlayPunt v = OverlayDinges.getGpsPlekken().get(a);
      System.out.println("Distance to " + v + ": " + v.minDistance);
      List<OverlayPunt> path = getShortestPathTo(v);
      System.out.println("Path: " + path);
    }

  }

  public static List<OverlayPunt> doeDijkstra(OverlayPunt veindpunt) {
    System.out.println("Dijkstra.doeDijkstra(" + veindpunt + ")");
    //OverlayPunt src = OverlayDinges.getDeLijnen().get(OverlayDinges.lijnDieHetDichtsteBijIs).punt1;
    OverlayPunt src = OverlayDinges.lijnDieHetDichtsteBijIsLijn.punt1;
    System.out.println("OverlayDinges.lijnDieHetDichtsteBijIsLijn: " + OverlayDinges.lijnDieHetDichtsteBijIsLijn);
    System.out.println("OverlayDinges.lijnDieHetDichtsteBijIsLijn.punt1: " + OverlayDinges.lijnDieHetDichtsteBijIsLijn.punt1);
    computePaths(src);
    //for (OverlayPunt v : vertices) {
    System.out.println("Aantal GPS-Plekken: " + OverlayDinges.getGpsPlekken().size());
    //for (int a = 0; a<OverlayDinges.getGpsPlekken().size(); a++){
    //OverlayPunt v = OverlayDinges.getGpsPlekken().get(a);
    System.out.println("Distance to " + veindpunt + ": " + veindpunt.minDistance);
    return getShortestPathTo(veindpunt);
    //System.out.println("Path: " + path);
    //}
  }
}
